﻿namespace AOTToolConsoleApp.Tools
{
    internal class DiskDataFillTool : ITool
    {
        public void Excute(string[] args)
        {
            if (args == null || args.Length == 0)
            {
                Write(Path.GetPathRoot(Environment.CurrentDirectory));
            }
            else
            {
                Parallel.For(0, args.Length, i =>
                {
                    string rootPath = Path.GetPathRoot(args[i]);
                    if (string.IsNullOrWhiteSpace(rootPath))
                    {
                        Console.WriteLine($"根路径\"{args[i]}\"无效(示例: D:\\)");
                        return;
                    }

                    Write(rootPath);
                });
                Console.WriteLine("\r\nall write over...");
            }
        }

        private static void Write(string rootPath)
        {
            try
            {
                string filePath = Path.Combine(rootPath, "xx.dat");
                using var fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
                byte[] buffer = new byte[4096];
                Console.WriteLine($"{filePath} start write...");
                while (true)
                {
                    try
                    {
                        fs.Write(buffer);
                        fs.Flush();
                    }
                    catch (IOException)
                    {
                        break;
                    }
                }
                Console.WriteLine($"{filePath} write over...");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }


}
